package TUILayer;
import java.util.Scanner;
import ControlLayer.*;

/**
 * Write a description of class PrivateCustomerUI here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class PrivateCustomerUI
{
    private PrivateCustomerCtr PriCtr;//reference til ControlLayer

    /**
     * Constructor for objects of class PrivateCustomerUI
     */
    public PrivateCustomerUI()
    {
        // initialise instance variables
        PriCtr = new PrivateCustomerCtr();
    }

    public void start()
    {
        PrivateCustomerMenu();
    }

    private void PrivateCustomerMenu()
    {
        boolean exit= false;

        while(!exit)
        {
            int choice = writePrivatePersonMenu();
            if(choice == 1)
            {
                createPerson();
                waitForInput();
            }
            else
            {
                if( choice == 2)
                {
                    findPerson();
                }
                else
                {
                    if(choice == 3)
                    {
                        deletePerson();
                    }
                    else
                    {
                        if(choice == 4)
                        {
                            updatePerson();
                        }
                        else
                        {
                            if( choice == 5)
                            {
                                showPersonInfo();
                                waitForInput();
                            }
                            else
                            {
                                if( choice == 6)
                                {
                                    //                                     showAllPersons();
                                    waitForInput();
                                }
                                else
                                {
                                    exit=true;
                                }
                            }
                        }
                    }
                }
            }
        }
    }

    private int writePrivatePersonMenu()
    {
        Scanner keyboard = new Scanner(System.in);
        System.out.println("\n *** privatkundeMenu***");
        System.out.println("(1) Tilføj person");
        System.out.println("(2) Find person");
        System.out.println("(3) Slet person");
        System.out.println("(4) Opdater personen");
        System.out.println("(5) Vis information om person");
        System.out.println("(6) List alle privatkunder");
        System.out.println("(7) Return");
        System.out.println("\n Foretag venligst et valg"); 
        int choise = keyboard.nextInt();
        return choise;

    }

    private long inputPersonsID()
    {
        boolean ok = false;
        long id = 0;
        while(!ok)
        {
            //input fra 
            Scanner keyboard = new Scanner(System.in);
            System.out.println(" indtast ID:  ");
            try
            {
                id = keyboard.nextLong();
                ok = true;
            }
            catch (Exception e)
            {
                System.out.println("fejl -  prøv igen");
                String input = keyboard.nextLine();
            }
        }//end while
        return id;
    }

    private int inputAccount()
    {
        boolean ok = false;
        int account = 0;
        while(!ok)
        {
            //input fra 
            Scanner keyboard = new Scanner(System.in);
            System.out.println(" indtast konto:  ");
            try
            {
                account = keyboard.nextInt();
                ok = true;
            }
            catch (Exception e)
            {
                System.out.println("fejl -  prøv igen");
                String input = keyboard.nextLine();
            }
        }//end while
        return account;
    }

    private void findPerson()
    {
        boolean ok = false;

        while(!ok)
        {
            try
            {
                String phoneNr = inputPhone();

                System.out.println("Navn : " + PriCtr.findPrivateCustomer(phoneNr).getName());
                System.out.println("Adresse : " + PriCtr.findPrivateCustomer(phoneNr).getAddress());
                System.out.println("Konto : " + PriCtr.findPrivateCustomer(phoneNr).getAccount());
                System.out.println("Telefon : " + PriCtr.findPrivateCustomer(phoneNr).getPhone());
                System.out.println("Email : " + PriCtr.findPrivateCustomer(phoneNr).getEmail());
                System.out.println("By : " + PriCtr.findPrivateCustomer(phoneNr).getCity());
                System.out.println("Post Nummer : " + PriCtr.findPrivateCustomer(phoneNr).getPostalCode());
                ok = true;
            }
            catch (Exception e)
            {
                System.out.println("fejl -  prøv igen");
            }
        }
    }

    private String inputName()
    {
        // makes an object keyboard to have input from the screen
        Scanner keyboard = new Scanner(System.in);  
        System.out.println(" Skriv Navn:  ");
        String name = keyboard.nextLine();
        return name;
    }                  

    private int inputPostalCode()
    {
        // makes an object keyboard to have input from the screen
        boolean ok = false;
        int code = 0;

        while(!ok)
        {
            //input fra 
            Scanner keyboard = new Scanner(System.in);//Scanner keyboard = new Scanner(System.in); 
            System.out.println(" indtast Post nummer:  ");//System.out.println(" indtast postnummer:  ");
            try{
                code = keyboard.nextInt();//int code = Integer.parseInt(keyboard.nextLine());
                ok = true;
            }
            catch (Exception e)
            {
                System.out.println("fejl -  prøv igen");
                String input = keyboard.nextLine();
            }
        }//end while
        return code;//return code;
    }           

    private String inputCity()
    {
        // makes an object keyboard to have input from the screen
        Scanner keyboard = new Scanner(System.in);  
        System.out.println("indtast By:  ");
        String city = keyboard.nextLine();
        return city;
    } 

    private String inputPhone()
    {
        // makes an object keyboard to have input from the screen
        Scanner keyboard = new Scanner(System.in);  
        System.out.println(" indtast Telefonnummer:  ");
        String phone = keyboard.nextLine();
        return phone;
    } 

    private String inputAddress()
    {
        // makes an object keyboard to read input from the console
        Scanner keyboard = new Scanner(System.in);
        System.out.println(" indtast Adressen: ");
        String address = keyboard.nextLine();
        return address;
    }

    private String inputEmail()
    {
        // makes an object keyboard to have input from the screen
        Scanner keyboard = new Scanner(System.in);  
        System.out.println(" Skriv Email:  ");
        String email = keyboard.nextLine();
        return email;
    }   

    private void deletePerson()
    {
        PriCtr.deleteCustomer(inputPhone());
    }

    private void showPersonInfo()
    {
        boolean ok = false;

        while(!ok)
        {
            try{
                String phoneNr = inputPhone();

                System.out.println("Navn : " + PriCtr.findPrivateCustomer(phoneNr).getName());
                System.out.println("Adresse : " + PriCtr.findPrivateCustomer(phoneNr).getAddress());
                System.out.println("Konto : " + PriCtr.findPrivateCustomer(phoneNr).getAccount());
                System.out.println("Telefon : " + PriCtr.findPrivateCustomer(phoneNr).getPhone());
                System.out.println("Email : " + PriCtr.findPrivateCustomer(phoneNr).getEmail());
                System.out.println("By : " + PriCtr.findPrivateCustomer(phoneNr).getCity());
                System.out.println("Post Nummer : " + PriCtr.findPrivateCustomer(phoneNr).getPostalCode());
                ok = true;
            }
            catch (Exception e)
            {
                System.out.println("fejl -  prøv igen");
            }
        }
    }

    private int updatePersonMenu()
    {
        Scanner keyboard = new Scanner(System.in);
        System.out.println("\f *** opdater Person Menu ***");
        System.out.println(" (1) Ændre navn");
        System.out.println(" (2) Ændre adresse");
        System.out.println(" (3) Ændre postnummer ");
        System.out.println(" (4) Ændre BY");
        System.out.println(" (5) Ændre telefonnummer");
        System.out.println(" (6) Return");
        System.out.print("\n foretag et valg: ");

        int choice = keyboard.nextInt();
        return choice;
    }

    private void updatePerson()
    {
        boolean exit = false;
        while(!exit)
        {
            int choice = updatePersonMenu(); 
            if(choice == 1)
            {   
                updateName();
            }
            else
            {
                if(choice == 2)
                {
                    updateAddress();
                }  
                else
                {
                    if(choice == 3)
                    {
                        updatePostalCode();
                    }  
                    else
                    {
                        if(choice == 4)
                        {
                            updateCity();
                        }
                        else

                        if(choice == 5)
                        {
                            updatePhone();
                        }
                        else
                        {
                            exit = true;
                        }
                    }//end else
                }//end else
            }//end else
        }//end else
    }

    //     private void showAllPersons()
    //     {
    //         PriCtr.printPersons();
    //     }

    private void createPerson()
    {
        PriCtr.createPrivateCustomer(inputAccount(), inputName(), inputAddress(), inputPhone(), inputPostalCode(), inputCity(), inputEmail());
    }

    private void waitForInput()
    {
        Scanner keyboard = new Scanner(System.in);
    }

    private void updateName()
    {
        boolean ok = false;
        while(!ok)
        {
            String tempPhone = inputPhone();
            if(PriCtr.findPrivateCustomer(tempPhone) == null)
            {
                System.out.println("fejl -  prøv igen");
            }
            else
            {
                PriCtr.setName(tempPhone, inputName());
                ok = true;
            }
        }
    }

    private void updateAddress()
    {
        boolean ok = false;
        while(!ok)
        {
            String tempPhone = inputPhone();
            if(PriCtr.findPrivateCustomer(tempPhone) == null)
            {
                System.out.println("fejl -  prøv igen");
            }
            else
            {
                PriCtr.setAddress(tempPhone, inputAddress());
                ok = true;
            }
        }
    }

    private void updatePostalCode()
    {
        boolean ok = false;
        while(!ok)
        {
            String tempPhone = inputPhone();
            if(PriCtr.findPrivateCustomer(tempPhone) == null)
            {
                System.out.println("fejl -  prøv igen");
            }
            else
            {
                PriCtr.setPostalCode(tempPhone, inputPostalCode());
                ok = true;
            }
        }
    }

    private void updateCity()
    {
        boolean ok = false;
        while(!ok)
        {
            String tempPhone = inputPhone();
            if(PriCtr.findPrivateCustomer(tempPhone) == null)
            {
                System.out.println("fejl -  prøv igen");
            }
            else
            {
                PriCtr.setCity(tempPhone, inputCity());
                ok = true;
            }
        }
    }

    private void updatePhone()
    {
        boolean ok = false;
        while(!ok)
        {
            String tempPhone = inputPhone();
            if(PriCtr.findPrivateCustomer(tempPhone) == null)
            {
                System.out.println("fejl -  prøv igen");
            }
            else
            {
                PriCtr.setPhone(tempPhone, inputPhone());
                ok = true;
            }
        }
    }
}

